var app = getApp();

Page({
  data: {
    selected: true,
    selected1: false,
    buyerState: 1,
    sellerState: 1,
    buyerMenu: [
      { id: '0', value: '全部', icon: 'icon-wenjian', checked: false },
      { id: '1', value: '进行中', icon: 'icon-clock', checked: true },
      { id: '2', value: '已取消', icon: 'icon-hand', checked: false },
      { id: '3', value: '已完成', icon: 'icon-caidan', checked: false }
    ],
    sellerMenu: [
      { id: '0', value: '全部', icon: 'icon-wenjian', checked: false },
      { id: '1', value: '进行中', icon: 'icon-clock', checked: true },
      { id: '2', value: '已取消', icon: 'icon-hand', checked: false },
      { id: '3', value: '已完成', icon: 'icon-caidan', checked: false }
    ],
    flag: true,
    p: 1,
    buyerOrders: [],
    isPullDown: false,
    isPullUp: false,
    isLoading: false,
    isPullUpText: '',
    page: 1,
    sellerOrders: [],
    isPullDown2: false,
    isPullUp2: false,
    isLoading2: false,
    isPullUpText2: '',
    lng:0,
    lat:0,
    province:"",
    city:""
  },
  onShow: function(options) {
    var that = this;
    that.resetData();
    that.getList();
    that.getListBe();
    app.getLocation(function (data) {
      that.setData({
        lng: data.lng,
        lat: data.lat,
        province: data.province,
        city: data.city
      })
    })
  },
  selected: function(e) {
    this.setData({
      selected1: false,
      selected: true,
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    })
  },
  selected1: function(e) {
    this.setData({
      selected: false,
      selected1: true,
      isPullDown2: false,
      isPullUp2: false,
      isLoading2: false,
      isPullUpText2: ''
    })
  },
  buyerMenuSelect: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var buyerMenu = that.data.buyerMenu;
    var buyerState = buyerMenu[index].id;
    for (var i = 0, len = buyerMenu.length; i < len; ++i) {
      if (i == index) {
        buyerMenu[i].checked = true;
      } else {
        buyerMenu[i].checked = false;
      }
    }
    that.setData({
      buyerMenu: that.data.buyerMenu,
      buyerState: buyerState,
      p: 1,
      buyerOrders: [],
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    });
    that.getList();
  },
  sellerMenuSelect: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var sellerMenu = that.data.sellerMenu;
    var sellerState = sellerMenu[index].id;
    for (var i = 0, len = sellerMenu.length; i < len; ++i) {
      if (i == index) {
        sellerMenu[i].checked = true;
      } else {
        sellerMenu[i].checked = false;
      }
    }
    that.setData({
      sellerMenu: that.data.sellerMenu,
      sellerState: sellerState,
      page: 1,
      sellerOrders: [],
      isPullDown2: false,
      isPullUp2: false,
      isLoading2: false,
      isPullUpText2: ''
    });
    that.getListBe();
  },
  onPullDownRefresh: function() {
    var that = this;
    if (that.data.selected) {
      if (that.data.isPullUp || that.data.isPullDown) {
        return
      }
      that.setData({
        p: 1,
        buyerOrders: [],
        isPullDown: true,
        isPullUp: false,
        isLoading: false,
        isPullUpText: ''
      });
      that.getList();
    } else {
      if (that.data.isPullUp2 || that.data.isPullDown2) {
        return
      }
      that.setData({
        page: 1,
        sellerOrders: [],
        isPullDown2: true,
        isPullUp2: false,
        isLoading2: false,
        isPullUpText2: ''
      });
      that.getListBe();
    }
    wx.stopPullDownRefresh();
  },
  onReachBottom: function() {
    var that = this;
    if (that.data.selected) {
      if (that.data.isPullUp || that.data.isPullDown) {
        return
      }
      that.setData({
        isPullUp: false,
        isPullDown: true,
        isLoading: true
      })
      that.getList();
    } else {
      if (that.data.isPullUp2 || that.data.isPullDown2) {
        return
      }
      that.setData({
        isPullUp2: false,
        isPullDown2: true,
        isLoading2: true
      })
      that.getListBe();
    }
  },
  getList: function() {
    var that = this;
    app.JsonPost("Api/Order/buyerOrders", {
      p: that.data.p,
      state: that.data.buyerState
    }, function(res) {
      if (res.list.length > 0) {
        var l = that.data.buyerOrders;
        for (var i = 0; i < res.list.length; i++) {
          l.push(res.list[i]);
        }
        that.setData({
          buyerOrders: l,
          p: that.data.p + 1,
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '没有更多数据了'
        });
      }
    })
  },
  getListBe: function() {
    var that = this;
    app.JsonPost("Api/Order/sellerOrders", {
      p: that.data.page,
      state: that.data.sellerState
    }, function(res) {
      if (res.list.length > 0) {
        var list = that.data.sellerOrders;
        for (var i = 0; i < res.list.length; i++) {
          list.push(res.list[i]);
        }
        that.setData({
          sellerOrders: list,
          page: that.data.page + 1,
          isPullUp2: false,
          isPullDown2: false,
          isPullUpText2: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp2: false,
          isPullDown2: false,
          isPullUpText2: '没有更多数据了'
        });
      }
    })
  },
  resetData: function () {
    var that = this;
    that.setData({
      p: 1,
      buyerOrders: [],
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: '',
      page: 1,
      sellerOrders: [],
      isPullDown2: false,
      isPullUp2: false,
      isLoading2: false,
      isPullUpText2: '',
      flag: true,
      cost: '',
      //notes: ''
    })
  },
  changeStatus: function (e) {
    var status = e.currentTarget.dataset.type;
    var orderId = e.currentTarget.dataset.id;
    var alertTip = e.currentTarget.dataset.tip;
    var that = this;
    if (status == 3 || status == '3') {
      //确定
      that.setData({
        flag: false,
        nowOrderId: orderId
      })
    } else {
      wx.showModal({
        title: '提示',
        content: alertTip,
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            var jsonData = {
              id: orderId,
              state: status,
              lng: that.data.lng,
              lat: that.data.lat,
              province: that.data.province,
              city: that.data.city
            };
            app.JsonPost("Api/Order/changeStatus", jsonData, function (res) {
              console.log(res);
              that.onShow();
            })
          }
        }
      });
    }
  },
  commentTap: function (e) {
    var orderid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'toComment?orderId=' + orderid
    })
  },
  commentView: function (e) {
    var comment = JSON.stringify(e.currentTarget.dataset.data);
    wx.navigateTo({
      url: 'comment?comment=' + comment
    })
  },
  costTap: function (e) {
    this.setData({
      cost: e.detail.value
    })
  },
  // notesTap: function (e) {
  //   this.setData({
  //     notes: e.detail.value
  //   })
  // },
  submitPop: function () {
    var that = this;
    var jsonData = {
      id: that.data.nowOrderId,
      state: 3,
      total: that.data.cost
    };
    app.JsonPost("Api/Order/changeStatus", jsonData, function (res) {
      console.log(res);
      that.onShow();
    })
  },
  resetPop: function () {
    this.setData({
      flag: true,
      cost: '',
      //notes: ''
    })
  },
  orderDetail: function (e) {
    var orderid = e.currentTarget.id;
    wx.navigateTo({
      url: 'orderDetail?orderId=' + orderid
    })
  },
  userCredit: function (e) {
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../user/credit?userId=' + userid
    })
  }
})